package com.prs.crm.action.person;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.person.Cultivate;
import com.prs.crm.domain.person.Degree;
import com.prs.crm.domain.person.Department;
import com.prs.crm.domain.person.Dimission;
import com.prs.crm.domain.person.Dossier;
import com.prs.crm.domain.person.DossierCultivate;
import com.prs.crm.domain.person.Education;
import com.prs.crm.domain.person.JobTypeTitle;
import com.prs.crm.domain.person.MarriageState;
import com.prs.crm.domain.person.PostSet;
import com.prs.crm.domain.person.RewardAndPunish;
import com.prs.crm.domain.person.StaffSex;
import com.prs.crm.domain.person.StaffState;
import com.prs.crm.domain.person.Weave;
import com.prs.crm.service.person.CultivateService;
import com.prs.crm.service.person.DegreeService;
import com.prs.crm.service.person.DepartmentService;
import com.prs.crm.service.person.DimissionService;
import com.prs.crm.service.person.DossierCultivateService;
import com.prs.crm.service.person.DossierService;
import com.prs.crm.service.person.EducationService;
import com.prs.crm.service.person.JobTypeTitleService;
import com.prs.crm.service.person.MarriageStateService;
import com.prs.crm.service.person.PostSetService;
import com.prs.crm.service.person.RewardAndPunishService;
import com.prs.crm.service.person.StaffSexService;
import com.prs.crm.service.person.StaffStateService;
import com.prs.crm.service.person.WeaveService;

public class DossierAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private Dossier dossier;
	private DossierService dossierService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private	List<Department> department;
	private DepartmentService departmentService;
	private List<PostSet> postSet;
	private PostSetService postSetService;
	private List<StaffSex> staffSex;
	private StaffSexService staffSexService;
	private List<MarriageState> marriageState;
	private MarriageStateService marriageStateService;
	private List<Education> education;
	private EducationService educationService;
	private List<Degree> degree;
	private DegreeService degreeService;
	private List<JobTypeTitle> jobTypeTitle;
	private JobTypeTitleService jobTypeTitleService;
	private List<StaffState> staffState;
	private StaffStateService staffStateService;
	private List<Weave> weave;
	private WeaveService weaveService;
	private List<Dimission> dimission;
	private DimissionService dimissionService;
	private File picImg;
	private String picImgFileName;
	private File resumeAttach;
	private String resumeAttachFileName;
	private Cultivate cultivate;
	private CultivateService cultivateService;
	private RewardAndPunish rewardAndPunish;
	private RewardAndPunishService rewardAndPunishService;
	private PaginationSupport paginationSupport;
	private DossierCultivate dossierCultivate;
	private DossierCultivateService dossierCultivateService;
	private String personId;
	
	public String getPeronid() {
		return personId;
	}
	public void setPeronid(String peronid) {
		this.personId = peronid;
	}
	public DossierCultivate getDossierCultivate() {
		return dossierCultivate;
	}
	public void setDossierCultivate(DossierCultivate dossierCultivate) {
		this.dossierCultivate = dossierCultivate;
	}
	public DossierCultivateService getDossierCultivateService() {
		return dossierCultivateService;
	}
	public void setDossierCultivateService(
			DossierCultivateService dossierCultivateService) {
		this.dossierCultivateService = dossierCultivateService;
	}
	public PaginationSupport getPaginationSupport() {
		return paginationSupport;
	}
	public void setPaginationSupport(PaginationSupport paginationSupport) {
		this.paginationSupport = paginationSupport;
	}
	public RewardAndPunish getRewardAndPunish() {
		return rewardAndPunish;
	}
	public void setRewardAndPunish(RewardAndPunish rewardAndPunish) {
		this.rewardAndPunish = rewardAndPunish;
	}
	public RewardAndPunishService getRewardAndPunishService() {
		return rewardAndPunishService;
	}
	public void setRewardAndPunishService(
			RewardAndPunishService rewardAndPunishService) {
		this.rewardAndPunishService = rewardAndPunishService;
	}
	public Cultivate getCultivate() {
		return cultivate;
	}
	public void setCultivate(Cultivate cultivate) {
		this.cultivate = cultivate;
	}
	public CultivateService getCultivateService() {
		return cultivateService;
	}
	public void setCultivateService(CultivateService cultivateService) {
		this.cultivateService = cultivateService;
	}

	private TreeNode root;
	
	public TreeNode getRoot() {
		return root;
	}
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public File getPicImg() {
		return picImg;
	}
	public void setPicImg(File picImg) {
		this.picImg = picImg;
	}
	public File getResumeAttach() {
		return resumeAttach;
	}
	public void setResumeAttach(File resumeAttach) {
		this.resumeAttach = resumeAttach;
	}
	public Dossier getDossier() {
		return dossier;
	}
	public void setDossier(Dossier dossier) {
		this.dossier = dossier;
	}
	public String getPicImgFileName() {
		return picImgFileName;
	}
	public void setPicImgFileName(String picImgFileName) {
		this.picImgFileName = picImgFileName;
	}
	public String getResumeAttachFileName() {
		return resumeAttachFileName;
	}
	public void setResumeAttachFileName(String resumeAttachFileName) {
		this.resumeAttachFileName = resumeAttachFileName;
	}
	public List<Department> getDepartment() {
		return department;
	}
	public void setDepartment(List<Department> department) {
		this.department = department;
	}
	public DepartmentService getDepartmentService() {
		return departmentService;
	}
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	public List<PostSet> getPostSet() {
		return postSet;
	}
	public void setPostSet(List<PostSet> postSet) {
		this.postSet = postSet;
	}
	public PostSetService getPostSetService() {
		return postSetService;
	}
	public void setPostSetService(PostSetService postSetService) {
		this.postSetService = postSetService;
	}
	public DossierService getDossierService() {
		return dossierService;
	}
	public void setDossierService(DossierService dossierService) {
		this.dossierService = dossierService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	
	public List<StaffSex> getStaffSex() {
		return staffSex;
	}
	public void setStaffSex(List<StaffSex> staffSex) {
		this.staffSex = staffSex;
	}
	public StaffSexService getStaffSexService() {
		return staffSexService;
	}
	public void setStaffSexService(StaffSexService staffSexService) {
		this.staffSexService = staffSexService;
	}
	public List<MarriageState> getMarriageState() {
		return marriageState;
	}
	public void setMarriageState(List<MarriageState> marriageState) {
		this.marriageState = marriageState;
	}
	public MarriageStateService getMarriageStateService() {
		return marriageStateService;
	}
	public void setMarriageStateService(MarriageStateService marriageStateService) {
		this.marriageStateService = marriageStateService;
	}
	public List<Education> getEducation() {
		return education;
	}
	public void setEducation(List<Education> education) {
		this.education = education;
	}
	public EducationService getEducationService() {
		return educationService;
	}
	public void setEducationService(EducationService educationService) {
		this.educationService = educationService;
	}
	public List<Degree> getDegree() {
		return degree;
	}
	public void setDegree(List<Degree> degree) {
		this.degree = degree;
	}
	public DegreeService getDegreeService() {
		return degreeService;
	}
	public void setDegreeService(DegreeService degreeService) {
		this.degreeService = degreeService;
	}
	public List<JobTypeTitle> getJobTypeTitle() {
		return jobTypeTitle;
	}
	public void setJobTypeTitle(List<JobTypeTitle> jobTypeTitle) {
		this.jobTypeTitle = jobTypeTitle;
	}
	public JobTypeTitleService getJobTypeTitleService() {
		return jobTypeTitleService;
	}
	public void setJobTypeTitleService(JobTypeTitleService jobTypeTitleService) {
		this.jobTypeTitleService = jobTypeTitleService;
	}
	public List<StaffState> getStaffState() {
		return staffState;
	}
	public void setStaffState(List<StaffState> staffState) {
		this.staffState = staffState;
	}
	public StaffStateService getStaffStateService() {
		return staffStateService;
	}
	public void setStaffStateService(StaffStateService staffStateService) {
		this.staffStateService = staffStateService;
	}
	public List<Weave> getWeave() {
		return weave;
	}
	public void setWeave(List<Weave> weave) {
		this.weave = weave;
	}
	public WeaveService getWeaveService() {
		return weaveService;
	}
	public void setWeaveService(WeaveService weaveService) {
		this.weaveService = weaveService;
	}
	public List<Dimission> getDimission() {
		return dimission;
	}
	public void setDimission(List<Dimission> dimission) {
		this.dimission = dimission;
	}
	public DimissionService getDimissionService() {
		return dimissionService;
	}
	public void setDimissionService(DimissionService dimissionService) {
		this.dimissionService = dimissionService;
	}
	@Action("listDossier")
	public String listDossier(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Dossier.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDossierService().listByPage(criteria, pagination));
		return SUCCESS;
	} 
	@Action("editDossier")
	public String editDossier(){
		if (ids!=null) {
			this.dossier=this.getDossierService().get(ids[0]);
		}
		this.department=this.departmentService.getAllDepartment();
		this.postSet=this.postSetService.getAllPostSet();
		this.staffSex=this.staffSexService.getAllStaffSex();
		this.marriageState=this.marriageStateService.getAllMarriageState();
		this.education=this.educationService.getAllEducation();
		this.degree=this.degreeService.getAllDegree();
		this.jobTypeTitle=this.jobTypeTitleService.getAllJobTypeTitle();
		this.staffState=this.staffStateService.getAllStaffStates();
		this.weave=this.weaveService.getAllWeave();
		this.dimission=this.dimissionService.getAllDimission();
		return SUCCESS;
	}
	@Action("viewDossier")
	public String viewDossier(){
		if(ids!=null){
			this.dossier=this.getDossierService().get(ids[0]);
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Cultivate.class);	
//		criteria.add(Restrictions.in("id",this.getDossierCultivateService().getDataperson(personId)));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDossierCultivateService().listByPage(criteria, pagination));

		DetachedCriteria cri=this.getPaginationHelper().createQuery(RewardAndPunish.class);
		paginationSupport=this.getPaginationHelper().prepare(paginationSupport);
		this.setPaginationSupport(this.getRewardAndPunishService().listByPage(cri, paginationSupport));
		return SUCCESS;
	}
	@Action(value="saveDossier",results={
			@Result(name="success",type="redirect",location="listDossier"),
			@Result(name="error",type="chain",location="editDossier")
	})
	public String saveDossier(){
		this.getDossierService().saveDossier(dossier, picImg, picImgFileName, resumeAttach, resumeAttachFileName);
		return SUCCESS;
	}
	@Action(value="removeDossier",results={
			@Result(name="success",type="redirect",location="listDossier"),
			@Result(name="error",type="chain",location="listDossier")
	})
	public String removeDossier(){
		if (ids==null) {
			this.addActionError("请选择数据行进行操作！");
			return ERROR;
		}
		this.getDossierService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Action(value = "loadDepartmentTree", results = { @Result(name = "success", type = "json",
			params = {"root","root"}) })
	public String loadTree(){
		root=new TreeNode();
		root.setId("-1");
		TreeNode node=new TreeNode();
		node.setId("0");
		node.setText("职员选择");
		
		Collection<Department> depart=this.getDepartmentService().getRootDepartment();
		for (Department departments : depart) {
			TreeNode nodes=new TreeNode();
			nodes.setText(departments.getName());
			nodes.setId("department-"+departments.getId().toString());
			node.addChildNode(nodes);
			addDepartmentNode(nodes, departments);
			addDossierNode(nodes, departments);
		}
		root.addChildNode(node);
		return SUCCESS;
	}
	
	public void addDossierNode(TreeNode parent,Department department){
		for (Dossier dossier : this.getDossierService().findByDepartment(department)) {
			TreeNode node=new TreeNode();
			if (dossier!=null) {
				node.setText(dossier.getName());
			}
			node.setId(dossier.getId().toString());
			parent.addChildNode(node);
		}
	}
	
	public void addDepartmentNode(TreeNode parent,Department department){
		for (Department depart : department.getChilds()) {
			TreeNode node=new TreeNode();
			node.setText(depart.getName());
			node.setId("department-"+depart.getId().toString());
			parent.addChildNode(node);
			addDepartmentNode(node, depart);
			addDossierNode(node, depart);
		}
	}
	
	@Action("selectDossier")
	public String selectDossier(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Dossier.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDossierService().listByPage(criteria, pagination));
		return SUCCESS;
	}
}